package com.drools.drools;

import com.drools.drools.model.Person;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.StatelessKieSession;

/**
 * Created by mishuai on 2019/4/9.
 */
public class BaseTest {

    protected KieSession getKieSession() {
        KieServices kieServices = KieServices.get();
        KieContainer kieContainer = kieServices.getKieClasspathContainer();
        KieSession kieSession = kieContainer.newKieSession("all-rules");

        return kieSession;
    }

    protected KieSession getKieSessionBySessionName(String sessionName) {
        KieContainer kieContainer = getKieContainer();
        KieSession kieSession = kieContainer.newKieSession(sessionName);
        return kieSession;
    }

    protected void fuctionKieSession(Object o,String ruleName){
        KieSession kieSession=this.getKieSessionBySessionName(ruleName);
        Person p=new Person();
        if(o instanceof Person){
            p=(Person)o;
        }
        kieSession.insert(p);
        System.out.println("fuction this object="+o.toString());
        int count=kieSession.fireAllRules();
        System.out.println("fuction count="+count);
        kieSession.dispose();
    }

    protected KieContainer getKieContainer(){
        KieServices kieServices = KieServices.get();
        KieContainer kieContainer = kieServices.getKieClasspathContainer();
        return kieContainer;
    }


    protected KieSession getKieSession(String agendaGroupName) {
        KieSession kieSession = getKieSession();
        kieSession.getAgenda().getAgendaGroup(agendaGroupName).setFocus();


        return kieSession;
    }
    protected StatelessKieSession getKieSessionByStatelessKieSessionName(String sessionName) {
        KieContainer kieContainer = getKieContainer();
        StatelessKieSession statelessKieSession=kieContainer.newStatelessKieSession(sessionName);
        return statelessKieSession;
    }
    protected StatelessKieSession getStatelessKieSession() {
        KieContainer kieContainer = getKieContainer();
        StatelessKieSession kieSession = kieContainer.newStatelessKieSession("stateless-rules");
        return kieSession;
    }
}
